package com.example.pceb.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pceb.entity.HighSkillTrainingSubsidy;
import com.example.pceb.entity.AwardProjectSubsidy;
import com.example.pceb.entity.StartupSocialInsuranceSubsidy;
import com.example.pceb.entity.SchoolAdmin;
import com.example.pceb.entity.School;
import com.example.pceb.mapper.HighSkillTrainingSubsidyMapper;
import com.example.pceb.mapper.AwardProjectSubsidyMapper;
import com.example.pceb.mapper.StartupSocialInsuranceSubsidyMapper;
import com.example.pceb.mapper.SchoolAdminMapper;
import com.example.pceb.mapper.SchoolMapper;
import com.example.pceb.service.SchoolAdminAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchoolAdminAuditServiceImpl implements SchoolAdminAuditService {

    @Autowired
    private SchoolAdminMapper schoolAdminMapper;
    
    @Autowired
    private SchoolMapper schoolMapper;
    
    @Autowired
    private HighSkillTrainingSubsidyMapper highSkillMapper;
    
    @Autowired
    private AwardProjectSubsidyMapper awardProjectMapper;
    
    @Autowired
    private StartupSocialInsuranceSubsidyMapper startupSocialMapper;

    @Override
    public List<HighSkillTrainingSubsidy> getHighSkillFormsBySchoolAdmin(String username) {
        // 1. 根据username查询学校管理员信息，获取school_id
        QueryWrapper<SchoolAdmin> adminWrapper = new QueryWrapper<>();
        adminWrapper.eq("username", username);
        SchoolAdmin schoolAdmin = schoolAdminMapper.selectOne(adminWrapper);
        
        if (schoolAdmin == null || schoolAdmin.getSchoolId() == null) {
            return null;
        }
        
        // 2. 根据school_id查询学校信息，获取schoolName
        School school = schoolMapper.selectById(schoolAdmin.getSchoolId());
        if (school == null) {
            return null;
        }
        
        // 3. 根据schoolName查询高技能人才培训补贴申请
        QueryWrapper<HighSkillTrainingSubsidy> formWrapper = new QueryWrapper<>();
        formWrapper.eq("school_name", school.getSchoolName());
        return highSkillMapper.selectList(formWrapper);
    }

    @Override
    public List<AwardProjectSubsidy> getAwardProjectFormsBySchoolAdmin(String username) {
        // 1. 根据username查询学校管理员信息，获取school_id
        QueryWrapper<SchoolAdmin> adminWrapper = new QueryWrapper<>();
        adminWrapper.eq("username", username);
        SchoolAdmin schoolAdmin = schoolAdminMapper.selectOne(adminWrapper);
        
        if (schoolAdmin == null || schoolAdmin.getSchoolId() == null) {
            return null;
        }
        
        // 2. 根据school_id查询学校信息，获取schoolName
        School school = schoolMapper.selectById(schoolAdmin.getSchoolId());
        if (school == null) {
            return null;
        }
        
        // 3. 根据schoolName查询获奖项目启动资金补贴申请
        QueryWrapper<AwardProjectSubsidy> formWrapper = new QueryWrapper<>();
        formWrapper.eq("school_name", school.getSchoolName());
        return awardProjectMapper.selectList(formWrapper);
    }

    @Override
    public List<StartupSocialInsuranceSubsidy> getStartupSocialFormsBySchoolAdmin(String username) {
        // 1. 根据username查询学校管理员信息，获取school_id
        QueryWrapper<SchoolAdmin> adminWrapper = new QueryWrapper<>();
        adminWrapper.eq("username", username);
        SchoolAdmin schoolAdmin = schoolAdminMapper.selectOne(adminWrapper);
        
        if (schoolAdmin == null || schoolAdmin.getSchoolId() == null) {
            return null;
        }
        
        // 2. 根据school_id查询学校信息，获取schoolName
        School school = schoolMapper.selectById(schoolAdmin.getSchoolId());
        if (school == null) {
            return null;
        }
        
        // 3. 根据schoolName查询初创企业社会保险补贴申请
        QueryWrapper<StartupSocialInsuranceSubsidy> formWrapper = new QueryWrapper<>();
        formWrapper.eq("school_name", school.getSchoolName());
        return startupSocialMapper.selectList(formWrapper);
    }

    @Override
    public boolean reviewHighSkillForm(Integer id, String status, String reviewComment) {
        HighSkillTrainingSubsidy form = highSkillMapper.selectById(id);
        if (form == null) {
            return false;
        }
        
        form.setStatus(status);
        form.setReviewComment(reviewComment);
        
        return highSkillMapper.updateById(form) > 0;
    }

    @Override
    public boolean reviewAwardProjectForm(Integer id, String status, String reviewComment) {
        AwardProjectSubsidy form = awardProjectMapper.selectById(id);
        if (form == null) {
            return false;
        }
        
        form.setStatus(status);
        form.setReviewComment(reviewComment);
        
        return awardProjectMapper.updateById(form) > 0;
    }

    @Override
    public boolean reviewStartupSocialForm(Integer id, String status, String reviewComment) {
        StartupSocialInsuranceSubsidy form = startupSocialMapper.selectById(id);
        if (form == null) {
            return false;
        }
        
        form.setStatus(status);
        form.setReviewComment(reviewComment);
        
        return startupSocialMapper.updateById(form) > 0;
    }
} 